package com.jinzo.mporaba;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.content.UriMatcher;

public class MporabaContentProvider extends ContentProvider {
    private DBAdapter db;
    private static final UriMatcher porabaUriMatcher;
    private static final int PORABA = 1;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
		db.open();
		int count = db.db.delete("usage", selection, selectionArgs);
		db.close();
		return count;
	}

	@Override
	public String getType(Uri uri) {
		if (porabaUriMatcher.match(uri) != PORABA) { 
			throw new IllegalArgumentException("Unknown URI " + uri); 
		} else {
			return QuotaItem.CONTENT_TYPE;
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
        ContentValues values;
        if (initialValues != null) {
        	values = new ContentValues(initialValues);
        } else {
        	values = new ContentValues();
        }
        QuotaItem qi = new QuotaItem(values.getAsString(DBAdapter.KEY_DISPLAYNAME), values.getAsBoolean(DBAdapter.KEY_PREPAID), values.getAsString(DBAdapter.KEY_UNIT), values.getAsString(DBAdapter.KEY_AVAILABLE), values.getAsString(DBAdapter.KEY_USED));
        db.open();
        long rowID = db.insertQuotaItem(qi);
        db.close();
        if (rowID > 0) {
        	Uri porabaUri = ContentUris.withAppendedId(QuotaItem.CONTENT_URI, rowID);
        	getContext().getContentResolver().notifyChange(porabaUri, null);
        	return porabaUri;
        } else {
        	throw new SQLException("Failed to insert row into " + uri);
        }
	}

	@Override
	public boolean onCreate() {
		this.db = new DBAdapter(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
		db.open();
		Cursor c = db.query(projection, selection, selectionArgs, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;

	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
		db.open();
		int count = db.db.update("usage", values, selection, selectionArgs);
		db.close();
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
	
	static {
		porabaUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		porabaUriMatcher.addURI("com.jinzo.mporaba.contentprovider", "poraba", PORABA);
	}
}

